var shopCarModel = require('../models/shopCar');
var bookModel = require('../models/books');
var shoucangModel = require('../models/shoucang');
var authorModel = require('../models/author');
var orderModel = require('../models/order');
const shopCar = {
    shopCar: (req, res, next) => {
        shopCarModel.find({ userId: req.session.loginUser._id, order_estate: '未完成' }).populate('book_id').then(doc => {
            res.render('shopCar', { book: doc, loginUser: req.session.loginUser })
        })
    },
    //加入购物车
    join: (req, res, next) => {
        bookModel.findOne({ _id: req.params.id }).populate('author_id').populate('colmun_id').then(doc => {
            shopCarModel.find({ book_id: req.params.id, order_estate: '未完成' }).populate("book_id").count().then(doc2 => {
                if (doc2 != 1) {
                    shopCarModel.create({ userId: req.session.loginUser._id, book_id: req.params.id, author: doc.author_id.name, colmun: doc.colmun_id.name, money: doc.money, name: doc.name, num: 1,img:doc.img }).then(doc => {
                        shopCarModel.find({ userId: req.session.loginUser._id, order_estate: '未完成' }).count().then(doc => {
                            res.json({
                                status: 1,
                                num: doc,
                                msg: '加入购物车成功'
                            })
                        })
                    }).catch(err => {
                        console.log(err);
                        res.json({
                            status: 0,
                            msg: '加入购物车失败'
                        })
                    })
                } else {
                    shopCarModel.find({ book_id: req.params.id, order_estate: '未完成' }).populate("book_id").then(doc3 => {
                        var num = parseInt(doc3[0].num)
                        shopCarModel.update({ book_id: req.params.id, order_estate: '未完成' }, { num: num + 1 }).populate("book_id").then(doc => {
                            shopCarModel.find({ userId: req.session.loginUser._id, order_estate: '未完成' }).count().then(doc => {
                                res.json({
                                    status: 1,
                                    num: doc,
                                    msg: '加入购物车成功'
                                })
                            })
                        }).catch(err => {
                            console.log(err);
                            res.json({
                                status: 0,
                                msg: '加入购物车失败'
                            })
                        })
                    })
                }
            })
        })
    },

    //收藏
    shoucang: (req, res, next) => {
        bookModel.find({ _id: req.params.id }).populate('author_id').populate('colmun_id').then(doc => {
            shoucangModel.find({ book_id: req.params.id }).populate('book_id').count().then(doc2 => {
                if (doc2 != 1) {
                    shoucangModel.create({ userId: req.session.loginUser._id, author: doc[0].author_id.name, colmun: doc[0].colmun_id.name, money: doc[0].money, name: doc[0].name, book_id: req.params.id }).then(doc => {
                        res.json({
                            status: 1,
                            msg: '收藏成功'
                        })
                    }).catch(err => {
                        console.log(err);
                        res.json({
                            status: 0,
                            msg: '收藏失败'
                        })
                    })
                } else {
                    res.json({
                        status: 1,
                        msg: '你已经收藏过这本书了'
                    })
                }
            })
        })
    },

    //取消收藏
    cancelCollect: (req, res, next) => {
        shoucangModel.deleteOne({ book_id: req.params.id }).populate('book_id').then(doc => {
            shoucangModel.find({ userId: req.session.loginUser._id }).populate('book_id').then(doc => {
                res.json({
                    status: 1,
                    result: doc,
                    msg: '取消收藏成功'
                })
            })
        }).catch(err => {
            res.json({
                status: 0,
                msg: err
            })
        })
    },

    //购物车书籍数量加减
    bookNum: (req, res, next) => {
        shopCarModel.updateOne({ _id: req.body.id }, { num: req.body.num }).then(doc => {
            res.json({
                status: 1,
                msg: '书籍数量修改成功'
            })
        }).catch(err => {
            res.json({
                status: 0,
                msg: '书籍数量修改失败'
            })
        })
    },

    //改变购物车选中商品订单状态
    checkBox: (req, res, next) => {
        if (req.body.check == 'allcheck') {
            shopCarModel.updateMany({ userId: req.session.loginUser._id, order_estate: '未完成' }, { order_estate: '已完成' }).then(doc => {
            })
        }
        if (req.body.check == 'allnocheck') {
            shopCarModel.updateMany({ userId: req.session.loginUser._id, order_estate: '已完成' }, { order_estate: '未完成' }).then(doc => {
            })
        }
        if (req.body.check == 'onecheck') {
            shopCarModel.update({ _id: req.body.id, userId: req.session.loginUser._id, order_estate: '未完成', }, { order_estate: '已完成' }).then(doc => {
            })
        }
        if (req.body.check == 'onenocheck') {
            shopCarModel.update({ _id: req.body.id, userId: req.session.loginUser._id, order_estate: '已完成', }, { order_estate: '未完成' }).then(doc => {
            })
        }
    },

    //生成订单
    order: (req, res, next) => {
        shopCarModel.find({ userId: req.session.loginUser._id, order_estate: '已完成', pay_estate: '未完成', is_new: 'new' }).then(doc => {
            orderModel.create({ number: Date.parse(new Date()), create_at: new Date().toLocaleString(), user_id: req.session.loginUser, info: doc }).then(doc => {
                shopCarModel.updateMany({ userId: req.session.loginUser._id, order_estate: '已完成', pay_estate: '未完成', is_new: 'new' }, { is_new: 'old' }).then(doc => {
                    res.json({
                        status: 1,
                        msg: '订单已生成'
                    })
                })
            })
        }).catch(err => {
            res.json({
                status: 0,
                msg: err
            })
        })
    },

    //移除商品
    moveBook: (req, res, next) => {
        shopCarModel.deleteOne({ _id: req.body.book_id }).then(d0c => {
            shopCarModel.find({ userId: req.session.loginUser._id, order_estate: '未完成' }).then(doc => {
                res.json({
                    status: 1,
                    book:doc,
                    msg: '移除商品成功'
                })
            })
        })
    },
    login: (req, res, next)=>{
        if (!req.session.loginUser){
            res.json({status:0})
        }else{
            res.render('shopCar', { book: doc, loginUser: req.session.loginUser })
        }
    }
}
module.exports = shopCar;